package com.landking.low;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.landking.R;
import com.landking.base.BaseFragment;

import java.lang.ref.SoftReference;


/**
 *
 */
public class LowFragment extends BaseFragment implements View.OnClickListener {

    /**
     * UI
     */
    private View mContentView;
    private FrameLayout containerFl;
    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;

    // String title = "标准页";// 需要改


    public static LowFragment newInstance() {
        Bundle args = new Bundle();
        LowFragment fragment = new LowFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static LowFragment newInstance(Bundle bundle) {
        Bundle args = bundle;
        LowFragment fragment = new LowFragment();
        fragment.setArguments(args);
        return fragment;
    }

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<LowFragment> fragmentRef;

        public MyHandler(LowFragment fragment) {
            fragmentRef = new SoftReference<LowFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            LowFragment fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();
            // 处理UI 变化
            switch (msg.what) {
                case 0:
                    //fragment.showAddProSuc(bundle);
                    break;
                case 1:
                    // fragment.showAdapter(bundle);
                    break;
                case 2:
                    // fragment.showTzAdapter(bundle);
                    break;
            }
        }
    }

    /**
     * data
     */
    public LowFragment() {
    }

    // 获取传递过来的参数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            // title = (String) arguments.get("title");// 需要改
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();

        // 不重复加载 做缓存处理
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.fragment_pic_layout, container, false);// 需要改
            this.initView();
            this.initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if (parent != null) {
            parent.removeView(mContentView);
        }

        return mContentView;
    }

    // 初始化视图
    private void initView() {
        containerFl = (FrameLayout) mContentView.findViewById(R.id.low_container);// 需要改
        backBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_back);
        confirmBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_confirm);
        backTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_back);
        titleTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_tv_title);
        confirmBtn.setVisibility(View.VISIBLE);
        backBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        titleTv.setOnClickListener(this);
    }

    private void initData() {
        titleTv.setText("测试页面");// 需要改
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_navigation_rl_back://  // 需要改
                /*LowFragment fragment2 = new LowFragment();
                if (fragment2 != null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("title", "LowFragment2");
                    fragment2.setArguments(bundle);
                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    transaction.replace(R.id.low_container, fragment2);// 需要改
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.addToBackStack(null);
                    transaction.commit();
                }*/
                break;
        }
    }
}